package org.go.plugin.self;

import org.go.expcetion.SchedulerConfigException;
import org.go.expcetion.SchedulerException;
import org.go.plugin.GoPlugin;
import org.go.scheduler.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ShutdownHookPlugin implements GoPlugin {
	private boolean cleanShutdown = true;
	private final Logger log = LoggerFactory.getLogger(getClass());

	@Override
	public void initialize(String name, final Scheduler scheduler) throws SchedulerConfigException {
		log.info("Registering Quartz shutdown hook.");
		String schedulerName = null;
		try {
			schedulerName = scheduler.getSchedulerName();
		} catch (SchedulerException e1) {
			e1.printStackTrace();
		}
		Thread t = new Thread("Quartz Shutdown-Hook " + schedulerName) {
			@SuppressWarnings("synthetic-access")
			@Override
			public void run() {
				log.info("Shutting down Quartz...");
				try {
					scheduler.shutdown(isCleanShutdown());
				} catch (SchedulerException e) {
					log.info("Error shutting down Quartz: " + e.getMessage(), e);
				}
			}
		};
		Runtime.getRuntime().addShutdownHook(t);

	}

	/**
	 * Determine whether or not the plug-in is configured to cause a clean
	 * shutdown of the scheduler.
	 * 
	 * <p>
	 * The default value is <code>true</code>.
	 * </p>
	 * 
	 * @see org.go.quartz.Scheduler#shutdown(boolean)
	 */
	public boolean isCleanShutdown() {
		return cleanShutdown;
	}

	/**
	 * Set whether or not the plug-in is configured to cause a clean shutdown
	 * of the scheduler.
	 * 
	 * <p>
	 * The default value is <code>true</code>.
	 * </p>
	 * 
	 * @see org.go.quartz.Scheduler#shutdown(boolean)
	 */
	public void setCleanShutdown(boolean b) {
		cleanShutdown = b;
	}

	@Override
	public void shutdown() {

	}

	@Override
	public void start() {

	}
}
